import type { UserInfo } from '@/types/api.types'
import { TT } from '@/service/token.ts'

export interface globalStoreState {
  userInfo: UserInfo
  token?: string
  theme: string
}

export const useGlobalStore = defineStore('globalStore', {
  state: (): globalStoreState => ({
    token: TT,

    // 用户信息
    userInfo: {
      box_pwd: null,
      box_status: null,
      email: null,
      google_auth_status: null,
      id: null,
      level: null,
      mobile: null,
      money: '',
      portrait: '',
      qq: '',
      skype: null,
      strong_box_amount: '',
      strong_box_status: null,
      telegram: '',
      user_group_id: null,
      user_nick: '',
      username: '',
      uuid: null,
      weixin: null,
      whatsapp: '',
      withdraw_pwd_status: null,
    },

    // 主题
    theme: 'u8',
  }),
  getters: {
    // 获取用户信息
    getUserInfo: (state) => state.userInfo,

    // 获取主题
    getTheme: (state) => state.theme,
  },
  actions: {
    setUserInfo(userInfo: UserInfo) {
      this.userInfo = userInfo
    },

    setThemeConfig(theme: string) {
      this.theme = theme
    },
    setToken(token?: string) {
      this.token = token
    },
  },
  persist: {
    storage: persistedState.localStorage,
    paths: ['theme', 'userInfo', 'token'],
  },
})

if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useGlobalStore, import.meta.hot))
}
